package com.kedll.library.adapter;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.google.android.flexbox.FlexboxLayoutManager;
import com.kedll.library.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by QiQi on 15/12/3.
 */
public class MultiTypeAdapter extends RecyclerView.Adapter<MultiTypeAdapter.ItemViewHolder> {

    private List<IItem> items;
    private OnItemClickListener onItemClickListener;
    private OnItemLongClickListener onItemLongClickListener;

    public MultiTypeAdapter() {
    }

    /**
     * 添加数据前先清数据列表
     *
     * @param data
     */
    public void addAllWithClear(List<? extends IItem> data) {
        if (items == null) {
            items = new ArrayList<>();
        }

        int currentSize = items.size();
        items.clear();
        notifyItemRangeRemoved(0, currentSize);
        items.addAll(data);
        notifyItemRangeInserted(0, data.size());
    }

    public List<IItem> getValues() {
        if (items == null) {
            items = new ArrayList<>();
        }
        return items;
    }

    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return ItemViewHolder.create(parent, viewType);
    }

    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {
        holder.bindTo(getItem(position));
        ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams();
        if (lp instanceof FlexboxLayoutManager.LayoutParams) {
            FlexboxLayoutManager.LayoutParams flexboxLp = (FlexboxLayoutManager.LayoutParams) lp;
            flexboxLp.setFlexGrow(1.0f);
        }
    }

    public IItem getItem(int position) {
        return CollectionUtils.get(items, position);
    }

    @Override
    public int getItemViewType(int position) {
        return getItem(position).getLayout();
    }

    @Override
    public int getItemCount() {
        return CollectionUtils.size(items);
    }

    public void setItem(IItem item) {
        clear();
        addItem(item);
    }

    public void clear() {
        if (items != null) {
            int size = items.size();
            items.clear();
            notifyItemRangeRemoved(0, size);

        }
    }

    public void addItem(IItem object) {
        if (items == null) {
            items = new ArrayList<>();
        }

        items.add(object);
    }

    public void setItem(int position, IItem item) {
        items.set(position, item);
        notifyItemChanged(position);
    }

    public void setItems(List<? extends IItem> items) {
        clear();
        addAll(items);
    }

    public void addAll(List<? extends IItem> data) {
        if (items == null) {
            items = new ArrayList<>();
        }
        int currentSize = items.size();
        items.addAll(data);
        notifyItemRangeInserted(currentSize, items.size());
    }

    public void addItem(int position, IItem obj) {
        if (items == null) {
            items = new ArrayList<>();
        }

        items.add(position, obj);
    }

    public int removeItem(IItem obj) {
        int position = findPos(obj);
        items.remove(obj);
        notifyItemRemoved(position);
        return position;
    }

    public int findPos(IItem item) {
        return items.indexOf(item);
    }

    public void removeItemRange(int startPosition, int itemCount) {
        for (int i = startPosition; i < itemCount + startPosition; i++) {
            items.remove(startPosition);
        }
    }

    public int removeItem(int position) {
        items.remove(position);
        return position;
    }

    public OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public <T> void setOnItemClickListener(OnItemClickListener<T> onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public OnItemLongClickListener getOnItemLongClickListener() {
        return onItemLongClickListener;
    }

    public <T> void setOnItemLongClickListener(OnItemLongClickListener<T> onItemLongClickListener) {
        this.onItemLongClickListener = onItemLongClickListener;
    }

    public interface IItem<T> {
        int getLayout();

        int getVariableId();

        T getData();
    }

    public interface OnItemClickListener<T> {
        void onItemClicked(View v, IItem<T> iItem);
    }

    public interface OnItemLongClickListener<T> {
        void onItemLongClicked(View v, IItem<T> iItem);
    }

    public static class ItemViewHolder extends RecyclerView.ViewHolder {

        private final ViewDataBinding binding;

        public ItemViewHolder(ViewDataBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        static ItemViewHolder create(ViewGroup parent, int viewType) {
            ViewDataBinding binding = DataBindingUtil.
                    inflate(LayoutInflater.from(parent.getContext()), viewType, parent, false);
            return new ItemViewHolder(binding);
        }

        public ViewDataBinding getBinding() {
            return binding;
        }

        public void bindTo(IItem item) {
            binding.setVariable(item.getVariableId(), item);
            binding.executePendingBindings();
        }
    }
}
